<?php

namespace app\admin\model;

use think\Model;
use think\facade\Request;

class AdminLoginErrorModel extends Model
{
    protected $table = '__ADMIN_LOGIN_ERROR__';

    //10分钟内连续输入错误次数
    public function loginError()
    {
        //先删除10分钟以前的所有数据
        $this->where(['error_time' => ['<', time() - 600]])->delete();
        $ip = Request::ip();
        //数据是否存在
        $res = $this->where(['login_ip' => $ip])->find();
        if (!empty($res)) {
            echo($res);
            $this->where(['id' => $res['id']])->setInc('login_error');
        } else {
            $this->save(['login_ip' => $ip, 'login_error' => 1, 'error_time' => time()]);
        }
    }

    /**
     * 验证用户登录密码错误次数
     * @param $username
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkLoginError()
    {
        $res = $this->where(['login_ip' => Request::ip()])->find();
        if (!empty($res) && $res['login_error'] >= 6 && (time() - $res['error_time']) < 600) {
            return ['status' => 'n', 'info' => '密码错误次数过多，10分钟后再试'];
        }
    }
}